package com.lz.football.module.activity.christmas
{
	import com.common.stdlib.DateUtil;
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.events.ActivityEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.activity.christmas.ChristmasVo;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.lz.football.module.activity.IChristmasView;
	
	import common.BaseView.DisplayMouseEventManager;
	import common.item.ComGoodsItem;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.core.TitleWindow;

	/**
	 *圣诞节活动view 
	 * @author lake
	 * 
	 */	
	public class ChristmasView extends TitleWindow implements IChristmasView
	{
		/**
		 *条目列表 
		 * 
		 */		
		private var itemList :Array = [];
		/**
		 *马到成功数 
		 */		
		private var txtSnowNum :TextField;
		/**
		 *增加马到成功按钮 
		 */		
		private var addBtn		:SimpleButton;
		/**
		 *活动时间 
		 */		
		private var txtTime	:TextField;
		
		public function ChristmasView()
		{
			super();
		}
		/**
		 *初始化 
		 * 
		 */
		public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.football.skin.christmasview");
			_symbolSkin.cacheAsBitmap = true;
			
			txtSnowNum = _symbolSkin.txtSnowNum;
			txtSnowNum.text = "";
			addBtn = _symbolSkin.addBtn;
			DisplayMouseEventManager.instance.addTipsDisplay(addBtn,"5点券购买一个马到成功");
			txtTime = _symbolSkin.txtTime;
			//初始化宽高
			width = _symbolSkin.width;
			height = _symbolSkin.height;
			//初始化UI界面控件
			windowBg = _symbolSkin.windowBg;//背景框
			windowBg.mouseChildren = false;
			//设置可拖动
			dragEnabled = true;
			this.addChild(_symbolSkin);
			closeBtn = Reflection.createInstance("com.lz.commonUI.closeBtn");
			this.addChild(closeBtn);
			closeBtn.x = 550;
			closeBtn.y = 35;
			//设置关闭按钮
			setCloseBtn(closeBtn);
			//add event
			addEvtListener();
			//初始化奖励
			var item :ComGoodsItem;
			var i :int = 0;
			var awards :Array = AppData.getInstance().activityDatemanager.christmasVo.awards;
			for each(var obp :Object in awards)
			{
				item = new ComGoodsItem();
				item.initUI();
				item.data = AppData.getInstance().goodsManager.getGoodsByTypeAndId(int(obp.type),int(obp.id));
				item.y = 255;
				item.x = 66 + i * 100;
				_symbolSkin.addChild(item);
				itemList.push(item);
				i ++ ;
			}
			setDataInfo();
			initShowDay();
		}
		/**
		 *初始化时间 
		 */		
		private function initShowDay():void
		{
			var startDate :Date = new Date("01/14/2014");//开始日期
			var cNum :int = 37;//活动天数
			var nextDate :Date;
			var timeMillons :Number;
			timeMillons = startDate.time + cNum * 24 * 60 * 60 * 1000 + 12 * 60 * 60 * 1000;
			nextDate = new Date(timeMillons);
			var timeStr :String = DateUtil.formatYearMonDayHSMString(startDate) + "-" + DateUtil.formatYearMonDayHSMString(nextDate);
			txtTime.htmlText = timeStr;
			txtTime.width = txtTime.textWidth + 20;
		}
		/**
		 *设置数据 
		 */		
		private function setDataInfo():void
		{
			var christmasVo :ChristmasVo = AppData.getInstance().activityDatemanager.christmasVo;
			txtSnowNum.text = "" + christmasVo.snowballs;
		}
		/**
		 *注册事件 
		 * 
		 */	
		public function addEvtListener():void
		{
			AppData.getInstance().activityDatemanager.christmasVo.addEventListener(Event.CHANGE,dataChangeHandler);
			addBtn.addEventListener(MouseEvent.CLICK,addSnowH);
			var i :int = 1;
			var tipsInfo :String = "";
			for(;i<=8;i++)
			{
				_symbolSkin["btn_"+i].addEventListener(MouseEvent.CLICK,getAwardH);
			}
			AppData.getInstance().activityDatemanager.christmasVo.addEventListener(ActivityEvent.ACTIVITY_CHRISTMAS_AWARD_RES,awardResH);
			tipsInfo = "有几率开出橙色球员包、橙色召集令、普通药剂";
			DisplayMouseEventManager.instance.addTipsDisplay(_symbolSkin["btn_"+6],tipsInfo);
			tipsInfo = "有几率开出巨星装备、橙色球员包、橙色召集令、橙色强化卡、普通药剂";
			DisplayMouseEventManager.instance.addTipsDisplay(_symbolSkin["btn_"+7],tipsInfo);
			tipsInfo = "有几率开出黑色召集令、黑色球员包、红色球衣、黑色强化卡、巅峰装备、\n7级属性卡、红色催化剂、欧元包（大）、声望包（大）、强效药剂";
			DisplayMouseEventManager.instance.addTipsDisplay(_symbolSkin["btn_"+8],tipsInfo);
		}
		/**
		 *数据变化 
		 * @param e
		 */		
		private function dataChangeHandler(e :Event):void
		{
			setDataInfo();
		}
		/**
		 *增加马到成功 
		 * @param e
		 * 
		 */		
		private function addSnowH(e :MouseEvent):void
		{
			var cashpoint :int = AppData.getInstance().clubDatamanager.clubInfo.cashpoint;
			if(cashpoint < 5)
			{
				Alert.showErrorTip("点券不足");
				return ;
			}
			Alert.show("您确定花费5点券购买一个马到成功吗？","",Alert.YES | Alert.NO,GlobalDef.AlertContainer,addSnowAlertH);
		}
		/**
		 *购买马到成功确认 
		 * @param type
		 * 
		 */		
		private function addSnowAlertH(type :int):void
		{
			if(Alert.YES == type)
			{
				this.dispatchEvent(new ActivityEvent(ActivityEvent.ACTIVITY_CHRISTMAS_ADD_SNOW));
			}
		}
		/**
		 *兑换 抽奖 
		 * @param e
		 * 
		 */		
		private function getAwardH(e :MouseEvent):void
		{
			var type :int = int(String(e.target.name).split("_")[1]);
			if(type <= 0)
			{
				return;
			}
			var snows :Array = [5,10,50,80,200];
			var christmasVo :ChristmasVo = AppData.getInstance().activityDatemanager.christmasVo;
			if(type <= 5)
			{
				if(christmasVo.snowballs < snows[type-1])
				{
					Alert.showErrorTip("马到成功不足");
					return;
				}
			}
			var cashpoint :int = AppData.getInstance().clubDatamanager.clubInfo.cashpoint;
			var coin :int = AppData.getInstance().clubDatamanager.clubInfo.coin;
			if(type == 6)
			{
				if(coin < 100000)
				{
					Alert.showErrorTip("欧元不足");
					return;
				}
			}
			if(type == 7)
			{
				if(cashpoint < 20)
				{
					Alert.showErrorTip("点券不足");
					return;
				}
			}
			if(type == 8)
			{
				if(cashpoint < 1000)
				{
					Alert.showErrorTip("点券不足");
					return;
				}
				else
				{
					Alert.show("您确定花费1000点券开启至尊宝箱吗？","",Alert.YES | Alert.NO,GlobalDef.AlertContainer,addOpenAlertH);
					return;
				}
			}
			this.dispatchEvent(new ActivityEvent(ActivityEvent.ACTIVITY_CHRISTMAS_AWARD,{type:type}));
		}
		/**
		 *购买马到成功确认 
		 * @param type
		 * 
		 */		
		private function addOpenAlertH(type :int):void
		{
			if(Alert.YES == type)
			{
				this.dispatchEvent(new ActivityEvent(ActivityEvent.ACTIVITY_CHRISTMAS_AWARD,{type:8}));
			}
		}
		/**
		 * onCloseHandler
		 * @param e
		 * 
		 */		
		override protected function onCloseHandler(e :MouseEvent):void
		{
			this.dispatchEvent(new ModuleEvent(ModuleEvent.REMOVE_MODULE));
		}
		/**
		 *奖励返回 
		 * @param e
		 * 
		 */		
		private var pid :int;
		private var ntype :int;
		private function awardResH(e :ActivityEvent):void
		{
			var type :int = int(e.param.type);
			pid = int(e.param.pid);
			ntype = int(e.param.ntype);
			var owfBtn :DisplayObject =  _symbolSkin["btn_"+type];
			var openEf :MovieClip = Reflection.createInstance("com.football.skin.openBoxEf");
			if(owfBtn && openEf)
			{
				_symbolSkin.mouseChildren = false;
				_symbolSkin.mouseEnabled = false;
				_symbolSkin.addChild(openEf);
				openEf.x = owfBtn.x + 26;
				openEf.y = owfBtn.y + 17;
				var item :ComGoodsItem = new ComGoodsItem();
				item.initUI();
				item.data = AppData.getInstance().goodsManager.getGoodsByTypeAndId(ntype,pid);
				DisplayObjectUtil.removeAllChildren(openEf.mc.itemMc.mc.itemCon);
				openEf.mc.itemMc.mc.itemCon.addChild(item);
				openEf.addEventListener(Event.REMOVED_FROM_STAGE,onROFF);
			}
		}
		/**
		 *开箱效果移除 
		 * @param e
		 * 
		 */		
		private function onROFF(e :Event):void
		{
			_symbolSkin.mouseChildren = true;
			_symbolSkin.mouseEnabled = true;
			e.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE,onROFF);
			showItemEf();
		}
		/**
		 *获得动画 
		 */		
		private var showEf :MovieClip ;
		private function showItemEf():void
		{
			showEf = Reflection.createInstance("com.football.skin.showItemEf");
			if(showEf && _symbolSkin)
			{
				_symbolSkin.addChild(showEf);
				showEf.x = 226;
				showEf.y = 185;
				var item :ComGoodsItem = new ComGoodsItem();
				item.initUI();
				var goods :GoodsVo = AppData.getInstance().goodsManager.getGoodsByTypeAndId(ntype,pid);
				item.data = goods;
				DisplayObjectUtil.removeAllChildren(showEf.mc.itemCom);
				showEf.mc.itemCom.addChild(item);
				showEf.mc.descMc.txtDesc.htmlText = "恭喜您！获得" + goods.name + "！";
			}
		}
		/**
		 *删除事件 
		 * 
		 */	
		public function removeEvtListener():void
		{
			closeBtn.removeEventListener(MouseEvent.CLICK,onCloseHandler);
			addBtn.removeEventListener(MouseEvent.CLICK,addSnowH);
			AppData.getInstance().activityDatemanager.christmasVo.removeEventListener(Event.CHANGE,dataChangeHandler);
			var i :int = 1;
			for(;i<=8;i++)
			{
				_symbolSkin["btn_"+i].removeEventListener(MouseEvent.CLICK,getAwardH);
				DisplayMouseEventManager.instance.removeTipsDisplay(_symbolSkin["btn_"+i]);
			}
			AppData.getInstance().activityDatemanager.christmasVo.removeEventListener(ActivityEvent.ACTIVITY_CHRISTMAS_AWARD_RES,awardResH);
		}
		/**
		 *销毁 
		 * 
		 */	
		public function destroyView():void
		{
			removeEvtListener();
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
			for each(var item :ComGoodsItem in itemList)
			{
				item.destroyView();
				item = null;
			}
			DisplayMouseEventManager.instance.removeTipsDisplay(addBtn);
			itemList = null;
			DisplayObjectUtil.removeAllChildren(this);
			_symbolSkin = null;
			closeBtn = null;
			showEf = null;
		}
		/**
		 *显示tab类型 
		 * @param $obj
		 * 
		 */
		public function showType($obj:Object):void
		{
		}
	}
}